$(function () {
    //从localStorage中获取customer
    var custStr = localStorage.getItem("customer");
    var customer = eval("("+ custStr +")");
    var data = "";
    var url;
    if(customer == null){
        alert("用户登陆超时，请重新登录");
        sessionStorage.removeItem("cart");
        window.location.href = "login/login.html";
    }
    var accountNo = customer.accountNo;
    // alert(accountNo);
    url = "orderInfo!showOrder";
    data += "accountNo=" + accountNo;
    sendRequest("post",url,data,showOrderBack);
});

function showOrderBack(result) {
    if(result == "\"no_item\""){

        alert("您还没有任何订单信息");
        window.location.href = "index.html";
    }

    sessionStorage.setItem("order",eval("(" + result + ")"));
    var rs = eval("("+ eval("("+ result +")") +")");
    console.log(rs);
    var str = "";
    //租用日期
    var rentTime;
    //计算已租天数
    var day;
    for(var i=0;i<rs.length;++i){
        rentTime = new Date(rs[i].rentTime.replace(/-/g,"/"));
        day = new Date().getTime() - rentTime.getTime();
        day = parseInt(day/(1000*60*60*24));
        str += "\t\t\t\t\t<li class=\"select\" >\n" +
            "\t\t\t\t\t\t<img src=\"data:image/jpg;base64,"+rs[i].productPhoto+"\" />\n" +
            "\t\t\t\t\t\t<div class=\"div_center\">\n" +
            "\t\t\t\t\t\t\t<h4>"+rs[i].productName+"</h4>\n" +
            "\t\t\t\t\t\t\t<span>"+rs[i].introduction+"</span>\n" +
            "\t\t\t\t\t\t\t<p class=\"now_value\">￥<b class=\"qu_su\">"+rs[i].rentPrice+"</b>/天</p>\n" +
            "\t\t\t\t\t\t\t<p class=\"now_value\">押金:￥<b class=\"rent_money\">"+rs[i].rentMoney+"</b></p>\n" +
            "\t\t\t\t\t\t\t<p class=\"now_value\" style='color: #2F4056'>已租用<b class=\"rent_money\">"+day+"</b>天</p>\n" +
            "\t\t\t\t\t\t</div>\n" +
            "<div day='"+ day +"' rent='"+ rs[i].rentPrice*day +"' time='"+ rs[i].rentTime +"' id='"+ rs[i].productQR +"' orders='"+ rs[i].rentOrderId +"' name='"+ rs[i].accountNo +"' onclick='fun(this)' class='div_right'><a class=\"settle_btn\" href=\"javascript:void(0);\" >归还商品</a></div>"+
            "\t\t\t\t\t</li>\n";
    }
    $(".commodity_box .commodity_list .commodity_list_term").html(str);
}

function fun(dt) {

    console.log($(dt).attr("id"));
    console.log($(dt).attr("orders"));
    console.log($(dt).attr("name"));
    console.log($(dt).attr("time"));
    console.log($(dt).attr("rent"));
    console.log($(dt).attr("day"));

    var data = "";
    data += "accountNo=" + $(dt).attr("name") +
            "&productQR=" + $(dt).attr("id") +
            "&rentOrderId=" + $(dt).attr("orders") +
            "&rentTime=" + $(dt).attr("time") +
            "&rent=" + $(dt).attr("rent") +
            "&realRentDays=" + $(dt).attr("day");
    var url = "/orderInfo!returnProduct";
    sendRequest("post",url,data,returnProductBack);
}

function returnProductBack(result) {
    if(result == "\"return_success\""){
        alert("归还成功，稍后工作人员会上门取货，祝您生活愉快！");
    }
    else{
        alert("归还失败，请结算所有费用！");
    }
}

function submitOrder() {
    //alert("提交订单-----");

    //封装订单信息
        //用户信息
    var pros = [];
    var custStr = localStorage.getItem("customer");
    var customer = eval("("+ custStr +")");
    //订单总价
    var totalMoney = $("#total_price b").html();
    if(customer == null){
        alert("用户登陆超时，请重新登录");
        window.location.href = "login/login.html";
    }
    var cart = sessionStorage.getItem("cart");
    var proInfo = eval("("+ cart +")").products;
    //console.log(proInfo);
    for(var i=0;i<proInfo.length;++i){
        //console.log($("#" + proInfo[i].productQR).hasClass("pitch_on"));
        if($("#" + proInfo[i].productQR).hasClass("pitch_on")){
            var value = $("#" + proInfo[i].productQR).parent().children(".div_right").children(".zi").html();
            proInfo[i].rentDays = value;
            proInfo[i].price = proInfo[i].rentMoney;
            pros.push(proInfo[i]);
            //alert(proInfo[i].productQR);
        }
    }

    //console.log(pros);



    if(pros.length <= 0){
        alert("请选择要租用的商品");
    }
    else{
        var proStr = JSON.stringify(pros);
        $.ajax({
            url:'/orderInfo!submitOrder',
            type:'post',
            data:{'custStr':custStr,
                'proStr':proStr,
                'totalMoney':totalMoney
            },
            //dataType:"json",
            success:function(data){
                //alert(data);
                //随便的显示一下传回的数据喽
                if(data == "insert_success"){
                    alert("租用成功，请等待发货！");
                }
                else
                    alert("租用失败！");
            }
        });
    }

}

function remove() {
    //封装订单信息
    //用户信息
    if(confirm("确认删除吗？")){
        var pros = [];
        var custStr = localStorage.getItem("customer");
        var customer = eval("("+ custStr +")");
        if(customer == null){
            alert("用户登陆超时，请重新登录");
            window.location.href = "login/login.html";
        }
        var cart = sessionStorage.getItem("cart");
        var proInfo = eval("("+ cart +")").products;
        //console.log(proInfo);
        for(var i=0;i<proInfo.length;++i){
            //console.log($("#" + proInfo[i].productQR).hasClass("pitch_on"));
            if($("#" + proInfo[i].productQR).hasClass("pitch_on")){

                pros.push(proInfo[i]);
                //alert(proInfo[i].productQR);
            }
        }

        console.log(pros);

        if(pros.length <= 0){
            alert("请选择要删除的商品");
        }else{
            var proStr = JSON.stringify(pros);
            $.ajax({
                url:'/rentCart!deleteItem',
                type:'post',
                data:{'accountNo':customer.accountNo,
                    'proStr':proStr
                },
                //dataType:"json",
                success:function(data){
                    //alert(data);
                    //随便的显示一下传回的数据喽
                    if(data == "success"){
                        alert("删除成功");
                        window.location.reload();
                    }
                }
            });
        }
    }

}